<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">

            <!-- 放置表单 -->
            <el-form ref="userInfoForm" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="userInfo" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password">
                <el-input v-model="userInfo.password" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click.native="update">更新</el-button>
                <el-button @click="$router.back()">返回</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <userInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <jobInfo />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailByIdApi } from '@/api/user'
import { saveEmployeeApi } from '@/api/employees'
import userInfo from './components/user-info.vue'
import jobInfo from './components/job-info.vue'
export default {
  components: {
    userInfo,
    jobInfo
  },
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      userId: this.$route.params.id,
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 12, message: '密码必须在6~12位之间', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // console.log(this.$route.params.id)
    const res = await getUserDetailByIdApi(this.userId)
    // console.log(res)
    this.userInfo = { ...res, password: '' }
  },
  methods: {
    async update() {
      console.log(this.$store.getters.id)
      await this.$refs.userInfoForm.validate()
      await saveEmployeeApi(this.userId, this.userInfo)
      // console.log(res)
      this.$message.success('修改成功')
      if (this.userInfo.id === this.$store.getters.id) {
        await this.$store.dispatch('user/logout')
        this.$message.warning('修改当前登录的密码,请重新登录')
        this.$router.push('/login')
      } else {
        this.$router.back()
      }
    }
  }
}
</script>

<style>

</style>
